/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.app.course.practice.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.common.mybatis.mapper.SqlMap;
import com.jeesite.common.mybatis.mapper.query.QueryDataScope;
import com.jeesite.common.mybatis.mapper.query.QueryOrder;
import com.jeesite.common.mybatis.mapper.query.QueryWhereEntity;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.app.course.practice.entity.TafterPractice;
import com.jeesite.modules.app.course.practice.service.TafterPracticeService;
import com.jeesite.modules.app.course.study.entity.TcourseStudy;
import com.jeesite.modules.conf.AppController;
import com.jeesite.modules.conf.MessageCode;

/**
 * 课后练习表Controller
 * @author zpp
 * @version 2020-01-26
 */
@Controller
@RequestMapping(value = "/app/course/practice/")
public class TafterPracticeController {

	@Autowired
	private TafterPracticeService tafterPracticeService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TafterPractice get(String id, boolean isNewRecord) {
		return tafterPracticeService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("app:course:practice:tafterPractice:view")
	@RequestMapping(value = {"list", ""})
	public String list(TafterPractice tafterPractice, Model model) {
		model.addAttribute("tafterPractice", tafterPractice);
		return "modules/app/course/practice/tafterPracticeList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("app:course:practice:tafterPractice:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TafterPractice> listData(TafterPractice tafterPractice, HttpServletRequest request, HttpServletResponse response) {
		tafterPractice.setPage(new Page<>(request, response));
		Page<TafterPractice> page = tafterPracticeService.findPage(tafterPractice);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("app:course:practice:tafterPractice:view")
	@RequestMapping(value = "form")
	public String form(TafterPractice tafterPractice, Model model) {
		model.addAttribute("tafterPractice", tafterPractice);
		return "modules/app/course/practice/tafterPracticeForm";
	}

	/**
	 * 保存课后练习表
	 */
	@RequiresPermissions("app:course:practice:tafterPractice:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TafterPractice tafterPractice) {
		tafterPracticeService.save(tafterPractice);
		return "保存课后练习表成功！";
	}
	
	/**
	 * 停用课后练习表
	 */
	@RequiresPermissions("app:course:practice:tafterPractice:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(TafterPractice tafterPractice) {
		tafterPractice.setStatus(TafterPractice.STATUS_DISABLE);
		tafterPracticeService.updateStatus(tafterPractice);
		return "停用课后练习表成功";
	}
	
	/**
	 * 启用课后练习表
	 */
	@RequiresPermissions("app:course:practice:tafterPractice:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(TafterPractice tafterPractice) {
		tafterPractice.setStatus(TafterPractice.STATUS_NORMAL);
		tafterPracticeService.updateStatus(tafterPractice);
		return "启用课后练习表成功";
	}
	
	/**
	 * 删除课后练习表
	 */
	@RequiresPermissions("app:course:practice:tafterPractice:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TafterPractice tafterPractice) {
		tafterPracticeService.delete(tafterPractice);
		return "删除课后练习表成功！";
	}
	@RequestMapping(value = "selectCourse")
	public void selectPractic(HttpServletRequest request,HttpServletResponse response){
		String userCode=request.getParameter("userCode");
		if(StringUtils.isBlank(userCode)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED,"用户信息未发现");
			return;
		}
		TafterPractice entity=new TafterPractice();
		entity.setUserId(userCode);
		//pageSize pageNo
		entity.setPage(new Page<>(request, response));
		try {
			Page<TafterPractice> findPage = tafterPracticeService.findPage(entity);
			JSONObject parseObject = JSONObject.parseObject(JsonMapper.toJson(findPage));
			AppController.renderSuccessObject(response, parseObject, MessageCode.SUCCESS_ACK);
		} catch (Exception e) {
			AppController.renderErrorString(response, MessageCode.SYSTEM_DB_ERROR, "");
			return;
		}
	}
}